<template>
  <div style="width: 500px;">
      <el-input
      placeholder="请输入部门名称进行搜索"
      v-model="filterText"
      prefix-icon="el-icon-search">
      </el-input>
      <el-tree
      :data="deps"
      :props="defaultProps"
      :filter-node-method="filterNode"
      :expand-on-click-node="false"
      ref="tree">
        <span class="custom-tree-node" style="display: flex;justify-content: space-between;width: 100%;"
              slot-scope="{node,data}">
          <span>{{node.label}}</span>
          <span>
            <el-button
              type="primary"
              size="mini"
              class="depBtn"
              style="padding: 3px"
              @click="()=>showAddDep(data)">
              添加部门
            </el-button>
            <el-button
              type="danger"
              size="mini"
              class="depBtn"
              style="padding: 3px"
              @click="()=>deleteDep(data)">
              删除部门
            </el-button>
          </span>
        </span>
    </el-tree>
    <el-dialog
      title="添加部门"
      :visible.sync="dialogVisible"
      v-model="dialogVisible"
      width="50%">
      <div>
        <table>
          <tr>
            <td>
              <el-tag>上级部门</el-tag>
            </td>
            <td>{{pname}}</td>
          </tr>
          <tr>
            <td>
              <el-tag>部门名称</el-tag>
            </td>
            <td>
              <el-input v-model="dep.name" placeholder="请输入部门名称"></el-input>
            </td>
          </tr>
        </table>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="doAddDep">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
     filterText: "",
     deps:[],
     defaultProps: {
       children: 'children',
       label: 'name'
     },
     dialogVisible: false,
     dep: {
       name: '',
       parentId: -1
     },
     pname: ''
    }
  },
  watch: {
     filterText(val) {
       this.$refs.tree.filter(val)
     }
  },
  mounted() {
    this.initDeps();
  },
  methods: {
    initDep () {
      this.dep = {
        name: '',
        parentId: -1
      }
      this.pname = ''
    },
    addDep2Deps(deps, dep) {
      for (let i = 0; i < deps.length; i++) {
        let d = deps[i];
        if (d.id == dep.parentId) {
          d.children = d.children.concat(dep);
          if (d.children.length > 0) {
            d.parent = true;
          }
          return;
        } else {
          this.addDep2Deps(d.children, dep);
        }
      }
    },
    doAddDep () {
      this.postRequest('/system/basic/department/', this.dep).then(resp => {
        if (resp) {
          this.addDep2Deps(this.deps, resp.data)
          this.dialogVisible = false
          this.initDep()
        }
      })
    },
    showAddDep (data) {
      this.dep.parentId = data.id
      this.pname = data.name
      this.dialogVisible = true
    },
    removeDepFromDeps (deps, id) {
      for (let i=0; i<deps.length; i++) {
        let d = deps[i]
        if (d.id==id) {
          deps.splice(i, 1)
          if (deps.length == 0) {
            d.isparent = false
          }
          return
        } else {
          this.removeDepFromDeps(d.children, id)
        }
      }
    },
    deleteDep (data) {
      if (data.isparent) {
        this.$message.error('删除失败')
      } else {
        this.deleteRequest('/system/basic/department/'+data.id).then(resp => {
          if (resp) {
            this.removeDepFromDeps(this.deps, data.id)
          }
        })
      }
      
    },
    initDeps () {
      this.getRequest('/system/basic/department/').then(resp => {
        if(resp) {
          this.deps = resp.data;
        }
      })
    },
     filterNode(value, data) {
       if (!value) return true
       return data.name.indexOf(value) !== -1
     }
  }
}
</script>

<style>
</style>
